package com.lzc.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lzc.blog.dao.mapper.SysUserMapper;
import com.lzc.blog.dao.pojo.SysUser;
import com.lzc.blog.service.LoginService;
import com.lzc.blog.service.SysUserService;
import com.lzc.blog.vo.ErrorCode;
import com.lzc.blog.vo.LoginUserVo;
import com.lzc.blog.vo.Result;
import com.lzc.blog.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private LoginService loginService;

    @Override
    public UserVo findUserVoById(Long id) {
        SysUser user = sysUserMapper.selectById(id);
        if (user==null){
            user=new SysUser();
            user.setId(1L);
            user.setAvatar("/static/img/logo.b3a48c0.png");
            user.setNickname("动力小子");
        }
        UserVo userVo=new UserVo();
        userVo.setId(String.valueOf(user.getId()));
        userVo.setAvatar(user.getAvatar());
        userVo.setNickname(user.getNickname());
        return userVo;
    }

    @Override
    public SysUser findUserById(Long id) {
        SysUser user = sysUserMapper.selectById(id);
        if (user==null){
            user=new SysUser();
            user.setNickname("寻血猎犬");
        }
        return user;
    }

    @Override
    public SysUser findUser(String account, String password) {
        LambdaQueryWrapper<SysUser> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount,account);
        queryWrapper.eq(SysUser::getPassword,password);
        queryWrapper.select(SysUser::getAccount,SysUser::getId,SysUser::getAvatar,SysUser::getNickname);
        queryWrapper.last("limit 1");
        return sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public Result findUserByToken(String token) {
        /**
         * 1.校验token合法性
         *   是否为空，解析是否成功 redis是否存在
         * 2.如果校验失败。返回错误
         * 3.如果成功，返回对应的结果 LoginUserVo
         */

       SysUser sysUser= loginService.checkToken(token);
       if (sysUser==null){
          return Result.fail(ErrorCode.TOKEN_ERROR.getCode(), ErrorCode.TOKEN_ERROR.getMsg());
       }
        LoginUserVo loginUserVo=new LoginUserVo(String.valueOf(sysUser.getId()),sysUser.getAccount(),sysUser.getNickname(),sysUser.getAvatar());
        return Result.success(loginUserVo);
    }

    /**
     * 保存用户信息
     * @param user
     */
    @Override
    public void save(SysUser user) {
        //id自动生成 默认生成id是 分布式id 雪花算法
        int insert = this.sysUserMapper.insert(user);


    }

    @Override
    public SysUser findUserByAccount(String account) {

        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount,account);
        queryWrapper.last("limit 1");
        SysUser user = this.sysUserMapper.selectOne(queryWrapper);
        return user;
    }
}
